import java.util.*;
import java.net.*;
import java.io.*;

public class TestRun {
	private static final String preurl = "http://202.119.47.8:8080/opac/search_adv_result.php?sType0=any&q0="; //These parts need not be modified regardless of user's choice
	private static final String midurl1 = "&pageSize=";
	private static final String midurl2 = "&sort=";
	private static final String midurl3 = "&desc=";
	private ArrayList<Book> books = new ArrayList<Book>();

	public static void main(String[] args) {
		TestRun tr = new TestRun();
		tr.go();
	}

	private void go() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			System.out.print("Enter the keyword and hit ENTER: ");
			String keyword = br.readLine();
			System.out.print("How many results do you desire? (Less than 50) ");
			String num = br.readLine();
			System.out
					.print("How will the result be sorted? (score/title/author/callno/publisher)");
			String sort = br.readLine();
			System.out.print("Desc or not? (true/false) ");
			String desc = br.readLine();
			URL req = new URL(preurl + keyword + midurl1 + num + midurl2 + sort
					+ midurl3 + desc);
			System.err.println("Requested URL is " + req);
			InputStream input = req.openStream(); //IO things
			process(input);
			input.close();
			for (int i = 0; i < books.size(); i++) {
				System.out.println(books.get(i).toString());
			}
		} catch (IOException ex) {
			System.out.flush();
			System.err
					.println("An exception occured. The program will shut down.");
			System.exit(1);
		}
	}

	private void process(InputStream input) throws IOException {
		boolean idfound = false;
		boolean titlefound = false;
		boolean authorfound = false;
		boolean publisherfound = false;
		boolean reqIDfound = false;
		String curtitle = "";
		String curauthor = "";
		String curpublisher = "";
		String curreqID = "";
		String curtype = "";
		String curmarc_no="";
		BufferedReader br = new BufferedReader(new InputStreamReader(input));
		while (br.ready()) {
			String cur = br.readLine();
			if (!(cur.contains("<TD  bgcolor=") && cur.contains("#FFFFFF"))) {  //Pretty easy, not expected
				idfound = false;
				titlefound = false;
				authorfound = false;
				publisherfound = false;
				reqIDfound = false;
				continue;
			} else {
				if (!idfound) {
					idfound = true;
					continue;
				}
				if (idfound && !titlefound) {
					titlefound = true;
					String[] temp = cur.split(">");
					curmarc_no = temp[1].split("marc_no=")[1].substring(0,temp[1].split("marc_no=")[1].length()-1);
					curtitle = temp[2].substring(0, temp[2].length() - 3);
					continue;
				}
				if (titlefound && !authorfound) {
					authorfound = true;
					String[] temp = cur.split(">");
					curauthor = temp[1].substring(0, temp[1].length() - 4);
					continue;
				}
				if (authorfound && !publisherfound) {
					publisherfound = true;
					String[] temp = cur.split(">");
					curpublisher = temp[1].substring(0, temp[1].length() - 4);
					continue;
				}
				if (publisherfound && !reqIDfound) {
					reqIDfound = true;
					String[] temp = cur.split(">");
					curreqID = temp[1].substring(0, temp[1].length() - 4);
					continue;
				}
				if (reqIDfound) {
					String[] temp = cur.split(">");
					curtype = temp[1].substring(0, temp[1].length() - 4);
					Book cur1 = new Book(curtitle, curauthor, curpublisher,
							curreqID, curtype,curmarc_no);
					this.books.add(cur1);
					idfound = false;
					titlefound = false;
					authorfound = false;
					publisherfound = false;
					reqIDfound = false;
					continue;
				}
			}
		}
	}
}
